﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crawler_v1.service.impl;
using Newtonsoft.Json;
using Crawler_v1.bean;
using System.Threading;
using System.Collections.Concurrent;
using System.Windows.Forms;

namespace Crawler_v1.spider
{
    class Queue
    {
        private int page = 0;
        private string baseUrl = "";


        private bool ended = false;

        private bool sendnew = false;

        private ConcurrentQueue<string> urls = new ConcurrentQueue<string>();

        private WebKitCrawler2 c2;

        private static Mutex mut = new Mutex();

        object syncLocker = new object();

        public Queue(Form form)
        {
            c2 = new WebKitCrawler2(form);
        }

        public void init()
        {
            page = 1;
            parseNewUrls(null);
        }

        public void parseNew(Action action)
        {
            page++;
            parseNewUrls(action);
        }

        public string getOneUrl()
        {
            string url;
            urls.TryDequeue(out url);
            return url;
        }

        public bool isEnded()
        {
            return ended;
        }

        public bool IsEmpty()
        {
            if (urls.Count <= 0)
            {
                //parseNewUrls();
                return true;
            }
            return false;
        }

        public void parseNewUrls(Action action)
        {
            mut.WaitOne();

            if (urls.Count > 0)
            {
                action();
                return;
            }

            c2.init("http://fec.mofcom.gov.cn/article/fwydyl/zgzx/");
            c2.onGet(new WebKitCrawler2.task(() =>
            {
                c2.addJs("columnlist.js");
                string str = c2.WebKitBrowser1.Document.InvokeScriptMethod("getColumnList", new object[] { }) as string;
                Console.WriteLine("obj:" + str);
                List<ListBean> beans = JsonConvert.DeserializeObject<List<ListBean>>(str);
                if (beans.Count > 0)
                {
                    foreach (ListBean bean in beans)
                    {
                        urls.Enqueue(bean.href);
                    }
                }
                else
                {
                    ended = false;
                }

            }));
            c2.onTaskFinish += new WebKitCrawler2.task(() =>
            {
                Console.WriteLine("taskFinish");
                mut.ReleaseMutex();
                if (action != null)
                {
                    action();
                }
                
            });

        }
    }
}
